package com.redpigedu.controller;


import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.redpigedu.entity.User;
import com.redpigedu.service.UserService;

/**
 * 管理员Controller类
 * 
 * @author redpigedu_code
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;

	/**
	 * 用户登录
	 * 
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/getByUserName")
	public ModelAndView getByUserName(HttpServletRequest request,HttpServletResponse response,User user) throws Exception {
		ModelAndView mav=new ModelAndView();
		User u = this.userService.getByUserName(user.getUsername());
		
		mav.setViewName("user");
		mav.addObject("user",u);
		return mav;
	}
	
	/**
	 * 查询所有用户
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/listAll")
	public ModelAndView listAll(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ModelAndView mav=new ModelAndView();
		List<User> users = this.userService.listAll();
		
		mav.setViewName("all");
		mav.addObject("users", users);
		return mav;
	}

	/**
	 * 查询所有用户
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateByUserName")
	public String updateByUserName(HttpServletRequest request,HttpServletResponse response,User user) throws Exception {
		int ret = this.userService.updateByUserName(user);
		
		if(ret==1){
			return "redirect:/user/listAll";	
		}
		
		return "redirect:/index.jsp";
	}
	
}
